package org.cogaen.sound;

/**
 * @class SoundFilter
 * @author Florian Lettner
 * 
 * An abstract class providing some interfaces to implement different audio filters.
 */
public abstract class SoundFilter {
	
	/**
	 * Resets the filter.
	 */
	public void reset() {

	}

	/**
	 * Provides the caller with the remaining size of the sample data.
	 * 
	 * @return The remaining size.
	 */
	public int getRemainingSize() {
		return 0;
	}

	/**
	 * Filters the provided byte stream.
	 * 
	 * @param sampledata The sound stream data.
	 */
	public void filter(byte[] sampledata) {
		filter(sampledata, 0, sampledata.length);
	}

	/**
	 * Filters the provided byte stream at a certain position for a certain length.
	 * 
	 * @param sampledata The sound stream data.
	 * @param offset The offset to start filtering.
	 * @param length The length of the sound stream.
	 */
	public abstract void filter(byte[] sampledata, int offset, int length);

	/**
	 * Gets a single sample of the provided sound stream.
	 * 
	 * @param buffer The buffer carrying the audio data.
	 * @param position The position of the sample to be extracted.
	 * @return The located sound sample.
	 */
	public static short getSample(byte[] buffer, int position) {
		return (short) (((buffer[position + 1] & 0xff) << 8) | (buffer[position] & 0xff));
	}

	/**
	 * Sets a single sample of the provided sound stream.
	 * 
	 * @param buffer The buffer carrying the audio data.
	 * @param position The position of the sample to be extracted.
	 */
	public static void setSample(byte[] buffer, int position, short sample) {
		buffer[position] = (byte) (sample & 0xff);
		buffer[position + 1] = (byte) ((sample >> 8) & 0xff);
	}
}
